Imports system.Xml.Serialization
Imports system.io
Public Class Actors
    Public Structure ActorsEntry
        <XmlAttribute()> Dim id As Long
        <XmlAttribute()> Dim name As String
        <XmlAttribute()> Dim flag As UInteger
        <XmlAttribute()> Dim classIdx As WowCrawler.SpellAndClassDB.ClassIndex

        <XmlAttribute()> Dim isRaid As Boolean
        <XmlAttribute()> Dim isEnemy As Boolean

        <XmlAttribute()> Dim mobId As Integer

        <XmlAttribute()> Dim isPet As Boolean
        <XmlAttribute()> Dim ownerId As Long
    End Structure

    Public Actors() As ActorsEntry

    Public Sub New()
        Actors = Nothing
    End Sub

    Protected Overrides Sub Finalize()
        Dim XU As New XmlUtils.AccessXml
        Try
            XU.ExportByXml(Actors, getXmlFilename("last_parse_actors.xml"))
        Catch ex As Exception
            Dim EH As New ErrorHandler
            EH.ErrorMsgBox(ex, "Actors.Finalize ExportByXml")
        End Try

        MyBase.Finalize()
    End Sub

    Private Function getXmlFilename(ByVal filename As String) As String
        Dim path As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
        Return My.Computer.FileSystem.CombinePath(path, filename)
    End Function

    Public Function ParseActor(ByVal id As Long, ByVal name As String, ByVal flag As UInteger) As Integer
        Dim ct As Integer = 0

        If Actors Is Nothing Then
            ct = 0
        Else
            Dim i As Long

            For i = 0 To Actors.Length - 1
                If Actors(i).id = id Then
                    Return i
                End If
            Next

            ct = Actors.Length
        End If

        ReDim Preserve Actors(ct)
        Actors(ct) = New ActorsEntry

        Actors(ct).id = id
        Actors(ct).name = name
        Actors(ct).flag = flag

        Dim isPet As Boolean = False
        Dim isRaid As Boolean = False
        Dim isEnemy As Boolean = False

        If (flag And &H3000) <> 0 Then
            isPet = True
        End If
        If (flag And &H7) > 0 Then
            isRaid = True
        End If
        If (flag And &H40) > 0 Then
            isEnemy = True
        End If

        Actors(ct).isPet = isPet
        Actors(ct).isRaid = isRaid
        Actors(ct).isEnemy = isEnemy

        Actors(ct).classIdx = WowCrawler.SpellAndClassDB.ClassIndex.Unknown

        If isPet = False Then
            Actors(ct).ownerId = id
        End If

        'Actors(ct).mobId = CInt("&h" & Mid(Hex(id), Len(Hex(id)) - 6 * 2 + 1, 6))
        Actors(ct).mobId = (id And &HFFFFFF000000) / &H1000000

        Return ct
    End Function

End Class
